<template>
  <div class="app-container">
    <div class="head-container">
      <!-- 搜索 -->
      <el-input
        v-model="listQuery.Filter"
        clearable
        size="small"
        placeholder="搜索..."
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-button
        class="filter-item"
        size="mini"
        type="success"
        icon="el-icon-search"
        @click="handleFilter"
        >搜索</el-button
      >
      <el-button
        class="filter-item"
        size="mini"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreate"
        >新增</el-button
      >
      <el-button
        class="filter-item"
        size="mini"
        type="success"
        icon="el-icon-edit"
        @click="handleUpdate()"
        >修改</el-button
      >
      <el-button
        slot="reference"
        class="filter-item"
        type="danger"
        icon="el-icon-delete"
        size="mini"
        @click="handleDelete()"
        >删除</el-button
      >


      <el-button
        class="filter-item"
        size="mini"
        type="success"
        icon="el-icon-edit"
        @click="handleupstatues()"
        >批量审核</el-button
      >

      <el-button class="filter-item" size="mini" type="info" icon="el-icon-download"
      @click="handleExport()">导出</el-button>


    </div>
    <el-dialog
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
      @close="cancel()"
      :title="formTitle"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        size="medium"
        label-width="100px"
        :inline="true"
      >
      <el-form-item label="订单标记" prop="orderHtml">
          <el-input
            v-model="form.orderHtml"
            placeholder="请输入订单编号"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>

        <el-form-item label="订单编号" prop="orderNo">
          <el-input
            v-model="form.orderNo"
            placeholder="请输入订单编号"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="下单店铺" prop="orderShop">
          <el-select v-model="form.orderShop" placeholder="please select your zone">
            <el-option v-for="item in ordershoplist" :label="item.label" :value="item.value" />

          </el-select>

        </el-form-item>
        <el-form-item label="下单客户" prop="orderName">
          <el-input
            v-model="form.orderName"
            placeholder="请输入下单客户"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="收件人" prop="recipients">
          <el-input
            v-model="form.recipients"
            placeholder="请输入收件人"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="orderPhone">
          <el-input
            v-model="form.orderPhone"
            placeholder="请输入手机"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="快递公司" prop="serverceCompany">

          <el-select v-model="form.serverceCompany" placeholder="please select your zone">
            <el-option v-for="item in serverceCompanylist" :label="item.label" :value="item.value" />

          </el-select>

        </el-form-item>
        <el-form-item label="仓库" prop="warehouseNo">
          <el-select
            v-model="form.warehouseNo"
            placeholder="please select your zone"
          >
            <el-option
              v-for="item in WarehouseOption"
              :label="item.warehouseName"
              :value="item.warehouseNo"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="业务员" prop="salesman">
          <el-input
            v-model="form.salesman"
            placeholder="请输入业务员"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="所在地区" prop="pCode">
          <el-cascader

            :props="cascaderProps"
            :options="treevalue"
            @change="handleChange"
          ></el-cascader>

        </el-form-item>
        <el-form-item label="固话" prop="orderTelphone">
          <el-input
            v-model="form.orderTelphone"
            placeholder="请输入固话"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="orderRemark">
          <el-input
            v-model="form.orderRemark"
            placeholder="请输入备注"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="配送方式" prop="orderStyle">
          <el-select v-model="form.orderStyle" placeholder="please select your zone">
            <el-option v-for="item in orderStylelist" :label="item.label" :value="item.value" />

          </el-select>

        </el-form-item>
        <el-form-item label="货到付款" prop="isDelivery">
          <el-select v-model="form.isDelivery" placeholder="请选择">
            <el-option label="是" value="是" />
            <el-option label="否" value="否" />
          </el-select>
        </el-form-item>
        <el-form-item label="详细地址" prop="orderAddr">
          <el-input
            v-model="form.orderAddr"
            placeholder="请输入详细地址"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="邮编" prop="orderEmail">
          <el-input
            v-model="form.orderEmail"
            placeholder="请输入邮编"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="运费" prop="orderFreight">
          <el-input
            v-model="form.orderFreight"
            placeholder="请输入运费"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="买家留言" prop="buyerRemark">
          <el-input
            v-model="form.buyerRemark"
            placeholder="请输入买家留言"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="下单时间" prop="orderDate">
          <el-time-picker
            v-model="form.orderDate"
            :style="{ width: '100%' }"
            placeholder="请选择下单时间"
            clearable
          ></el-time-picker>
        </el-form-item>
        <el-form-item label="是否预售" prop="ispresell">
          <el-select
            v-model="form.ispresell"
            placeholder="请选择预售订单"
            clearable
            :style="{ width: '100%' }"
          >
          <el-option label="是" value="是" />
          <el-option label="否" value="否" />
          </el-select>
        </el-form-item>
        <el-form-item label="预计发货时间" prop="deliverytime">
          <el-time-picker
            v-model="form.deliverytime"
            :style="{ width: '100%' }"
            placeholder="请选择预计发货时间"
            clearable
          ></el-time-picker>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button size="small" type="text" @click="cancel">取消</el-button>
        <el-button
          size="small"
          v-loading="formLoading"
          type="primary"
          @click="save"
          >确认</el-button
        >
      </div>
    </el-dialog>
    <el-table
      ref="multipleTable"
      v-loading="listLoading"
      :data="list"
      size="small"
      style="width: 90%;"
      @sort-change="sortChange"
      @selection-change="handleSelectionChange"
      @row-click="handleRowClick"
    >
      <el-table-column type="selection" width="44px"></el-table-column>
      <el-table-column label="订单标记" prop="orderHtml" align="center" >
        <template slot-scope="scope">
          <el-tag type="primary">{{ scope.row.orderHtml }}</el-tag>
        </template>

        </el-table-column>
      <el-table-column label="订单编号" prop="orderNo" align="center" />
      <el-table-column label="下单店铺" prop="orderShop" align="center" />
      <el-table-column label="下单客户" prop="orderName" align="center" />
      <el-table-column label="收件人" prop="recipients" align="center" />
      <el-table-column label="手机" prop="orderPhone" align="center" />
      <el-table-column label="快递公司" prop="serverceCompany" align="center" />
      <el-table-column label="仓库" prop="warehouseName" align="center" />
      <el-table-column label="业务员" prop="salesman" align="center" />
      <el-table-column label="所在地区" prop="regionName" align="center" />
      <el-table-column label="固话" prop="orderTelphone" align="center" />
      <el-table-column label="备注" prop="orderRemark" align="center" />
      <el-table-column label="配送方式" prop="orderStyle" align="center" />
      <el-table-column label="货到付款" prop="isDelivery" align="center" />
      <el-table-column label="详细地址" prop="orderAddr" align="center" />
      <el-table-column label="邮编" prop="orderEmail" align="center" />
      <el-table-column label="运费" prop="orderFreight" align="center" />
      <el-table-column label="买家留言" prop="buyerRemark" align="center" />
      <el-table-column label="审核状态" prop="orderStatues" align="center" >
        <template v-slot="scope">
          {{scope.row.orderStatues===false?'待审核':'已审核'}}


        </template>
        </el-table-column>
      <el-table-column label="下单时间" prop="orderDate" align="center" >
        <template v-slot="scope">
          {{scope.row.orderDate.substring(0,10)}}
        </template>
        </el-table-column>
      <el-table-column label="是否预售" prop="ispresell" align="center" >

        </el-table-column>
      <el-table-column
        label="预计发货时间"
        prop="deliverytime"
        align="center"
      >

      <template v-slot="scope">
          {{scope.row.deliverytime.substring(0,10)}}
        </template>
        </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <el-button
            type="primary"
            size="mini"
            @click="handleUpdate(row)"
            icon="el-icon-edit"
          />
          <el-button
            type="danger"
            size="mini"
            @click="handleDelete(row)"
            icon="el-icon-delete"
          />

          <el-button
            type="danger"
            size="mini"
            @click="handleupstatues(row)"

          >
          确认审核
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="totalCount > 0"
      :total="totalCount"
      :page.sync="page"
      :limit.sync="listQuery.MaxResultCount"
      @pagination="getList"
    />
  </div>
</template>
<script>
import Pagination from "@/components/Pagination";
import permission from "@/directive/permission/index.js";
import axios from "axios";
const defaultForm = {
  id: null,
  orderNo: null,
  orderShop: null,
  orderName: null,
  recipients: null,
  orderPhone: null,
  serverceCompany: null,
  warehouseNo: null,
  salesman: null,
  pCode: null,
  orderTelphone: null,
  orderRemark: null,
  orderStyle: null,
  isDelivery: null,
  orderAddr: null,
  orderEmail: null,
  orderFreight: null,
  buyerRemark: null,
  orderDate: null,
  ispresell: null,
  deliverytime: null,
  orderStatues:false,

  orderHtml:null
};
export default {
  name: "Orders",
  components: {
    Pagination
  },
  directives: {
    permission
  },
  props: [],
  data() {
    return {
      rules: {
        OrderNo: [
          {
            required: true,
            message: "请输入订单编号",
            trigger: "blur"
          }
        ],
        OrderShop: [
          {
            required: true,
            message: "请输入下单店铺",
            trigger: "blur"
          }
        ],
        OrderName: [],
        Recipients: [
          {
            required: true,
            message: "请输入收件人",
            trigger: "blur"
          }
        ],
        OrderPhone: [
          {
            required: true,
            message: "请输入手机",
            trigger: "blur"
          }
        ],
        ServerceCompany: [],
        WarehouseNo: [],
        Salesman: [],
        PCode: [
          {
            required: true,
            message: "所在地区",
            trigger: "blur"
          }
        ],
        OrderTelphone: [],
        OrderRemark: [],
        OrderStyle: [],
        IsDelivery: [],
        OrderAddr: [
          {
            required: true,
            message: "请输入详细地址",
            trigger: "blur"
          }
        ],
        OrderEmail: [],
        OrderFreight: [
          {
            required: true,
            message: "请输入运费",
            trigger: "blur"
          }
        ],
        BuyerRemark: [],
        OrderDate: [
          {
            required: true,
            message: "请选择下单时间",
            trigger: "change"
          }
        ],
        Ispresell: [],
        Deliverytime: []
      },
      form: Object.assign({}, defaultForm),
      list: null,
      totalCount: 0,
      shuzucode: [],
      listLoading: true,
      formLoading: false,
      listQuery: {
        Filter: "",
        Sorting: "",
        SkipCount: 0,
        MaxResultCount: 10
      },
      page: 1,
      dialogFormVisible: false,
      multipleSelection: [],
      WarehouseOption: [],
      treevalue: [],
      ordershoplist:[],
      orderStylelist:[],
      serverceCompanylist:[],
      formTitle: "",
      isEdit: false,
      IspresellOptions: [
        {
          label: "否",
          value: "2"
        },
        {
          label: "是",
          value: "1"
        }
      ],
      // 配置cascader属性
      cascaderProps: {
        value: "value",
        label: "label",
        children: "children"
      }
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getList();
    this.getWarehouse();
    this.getcascader();
    this.getordershop();
    this.getexpresscompany();
    this.getdistribution();
  },
  mounted() {},
  methods: {
    getList() {
      this.listLoading = true;
      this.listQuery.SkipCount =
        (this.page - 1) * this.listQuery.MaxResultCount;
      this.$axios.gets("api/business/Orders", this.listQuery).then(response => {
        this.list = response.items;
        this.totalCount = response.totalCount;
        this.listLoading = false;
        console.log(response);
      });
    },



    //绑定仓库
    getWarehouse() {
      this.$axios.gets("api/business/Warehouse/sdsdsd").then(response => {
        this.WarehouseOption = response;
        console.log(this.WarehouseOption);
      });
    },
    //下单店铺字典绑定
    getordershop(){
      this.$axios.gets("api/base/dictDetails/list?name=OrderShop").then(response => {
        this.ordershoplist = response.items;
      });
    },


    //快递公司字典绑定
    getexpresscompany() {
      this.$axios.gets("api/base/dictDetails/list?name=ServerceCompany").then(response => {
        this.serverceCompanylist = response.items;
      });
    },


    //配送方式字典绑定
    getdistribution() {
      this.$axios.gets("api/base/dictDetails/list?name=OrderStyle").then(response => {
        this.orderStylelist = response.items;
      });
    },
    //级联
    getcascader() {
      this.$axios.gets("api/business/Province/tree").then(response => {
        this.treevalue = response.items;
        console.log(this.treevalue);
      });
    },
    handleChange(value) {
      console.log('Selected values:', value);
      console.log(value[2])
      this.form.PCode = value[2];
    },
    fetchData(id) {
      this.$axios.gets("api/business/Orders/" + id).then(response => {
        this.form = response;
        console.log(this.form);

      });
    },
    handleFilter() {
      this.page = 1;
      this.getList();
    },
    handleCreate() {
      this.formTitle = "新增表单";
      this.isEdit = false;
      this.dialogFormVisible = true;
    },
    //删除
    handleDelete(row) {
      var params = [];
      let alert = "";
      if (row) {
        params.push(row.id);
        alert = row.name;
      } else {
        if (this.multipleSelection.length === 0) {
          this.$message({
            message: "未选择",
            type: "warning"
          });
          return;
        }
        this.multipleSelection.forEach(element => {
          let id = element.id;
          params.push(id);
        });
        alert = "选中项";
      }
      this.$confirm("是否删除" + alert + "?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$axios
            .posts("api/business/Orders/delete", params)
            .then(response => {
              this.$notify({
                title: "成功",
                message: "删除成功",
                type: "success",
                duration: 2000
              });
              this.getList();
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },

    //审核
    handleupstatues(row){
      var params = [];
      let alert = "";
      if (row) {
        params.push(row.id);
        alert = row.name;
      } else {
        if (this.multipleSelection.length === 0) {
          this.$message({
            message: "未选择",
            type: "warning"
          });
          return;
        }
        this.multipleSelection.forEach(element => {
          let id = element.id;
          params.push(id);
        });
        alert = "选中项";
      }
      this.$confirm("是否修改" + alert + "?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$axios
            .posts("api/business/Orders/updatestatues", params)
            .then(response => {
              this.$notify({
                title: "成功",
                message: "审核成功",
                type: "success",
                duration: 2000
              });
              this.getList();
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消修改"
          });
        });
    },
    //修改
    handleUpdate(row) {
      this.formTitle = "修改表单";
      this.isEdit = true;
      if (row) {
        this.fetchData(row.id);
        this.dialogFormVisible = true;
      } else {
        if (this.multipleSelection.length != 1) {
          this.$message({
            message: "编辑必须选择单行",
            type: "warning"
          });
          return;
        } else {
          this.fetchData(this.multipleSelection[0].id);
          this.dialogFormVisible = true;
        }
      }
    },
    save() {
      console.log(this.shuzucode[2]);
      // this.form.PCode=this.shuzucode[2]
      // console.log(this.form.PCode)
      this.$refs.form.validate(valid => {
        if (valid) {
          this.formLoading = true;
          this.form.roleNames = this.checkedRole;
          if (this.isEdit) {
            this.$axios
              .posts("api/business/Orders/data-post", this.form)
              .then(response => {
                this.formLoading = false;
                this.$notify({
                  title: "成功",
                  message: "更新成功",
                  type: "success",
                  duration: 2000
                });
                this.dialogFormVisible = false;
                this.getList();
              })
              .catch(() => {
                this.formLoading = false;
              });
          } else {
            this.$axios.posts('api/business/Orders/data-post', this.form).then(response => {
              this.formLoading = false;
              this.$notify({
                title: '成功',
                message: '新增成功',
                type: 'success',
                duration: 2000
              });
              this.dialogFormVisible = false;
              this.getList();
            }).catch(() => {
              this.formLoading = false;
            });
          }
        }
      });
    },


    //导出
    handleExport(){
      axios({
                url: 'http://localhost:51186/api/business/Orders/ExportAll', // 请将此 URL 替换为你的服务器导出 Excel 的接口地址
                method: 'GET',
                params: {
                  OrderNo: this.form.OrderNo,
                  // OrderShop: this.form.OrderShop,
                  // OrderName: this.form.OrderName,
                  // Recipients: this.form.Recipients,
                  // OrderPhone: this.form.OrderPhone,
                  // ServerceCompany:this.form.ServerceCompany,
                  // WarehouseName:this.form.WarehouseName,
                  // Salesman:this.form.Salesman,
                  // RegionName:this.form.RegionName,
                  // OrderTelphone:this.form.OrderTelphone,
                  // OrderRemark:this.form.OrderRemark,
                  // OrderStyle:this.form.OrderStyle,
                  // IsDelivery:this.form.IsDelivery,
                  // OrderAddr:this.form.OrderAddr,
                  // OrderEmail:this.form.OrderEmail,
                  // BuyerRemark:this.form.BuyerRemark,
                  // OrderDate:this.form.OrderDate,
                  // Ispresell:this.form.Ispresell,
                  // Deliverytime:this.form.Deliverytime,
                },
                responseType: 'blob'
            }).then((response) => {
                const blob = new Blob([response.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
                const url = window.URL.createObjectURL(blob);
                const a = document.createElement('a');
                a.href = url;
                a.download = '订单信息.xlsx';
                a.click();
                window.URL.revokeObjectURL(url);
                this.$notify({
                    title: '成功',
                    message: '导出成功',
                    type: 'success',
                    duration: 2000
                });
            }).catch((error) => {
                console.error('导出 Excel 时出错:', error);
            });
    },



    sortChange(data) {
      const { prop, order } = data;
      if (!prop || !order) {
        this.handleFilter();
        return;
      }
      this.listQuery.Sorting = prop + " " + order;
      this.handleFilter();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleRowClick(row, column, event) {
      this.$refs.multipleTable.clearSelection();
      this.$refs.multipleTable.toggleRowSelection(row);
    },
    cancel() {
      this.form = Object.assign({}, defaultForm);
      this.dialogFormVisible = false;
      this.$refs.form.clearValidate();
    }
  }
};
</script>
<style></style>
